package servlet;

import entity.PersonalFile;
import service.PersonalFileServiceImpl;
import util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project javase2203
 * @Package servlet
 * @Description:
 * @Date 2022/5/27 14:25
 */
@WebServlet("/addPersonalFile")
public class AddPersonalFileServlet extends HttpServlet {



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        HttpSession session = request.getSession();

        //获取请求参数

        String name = request.getParameter("name");
        String birthday = request.getParameter("birthday");
        String gender = request.getParameter("gender");
        String career = request.getParameter("career");
        String address = request.getParameter("address");
        String mobile = request.getParameter("mobile");
        String picture = request.getParameter("picture");

        PersonalFile personalFile = new PersonalFile(null, name,birthday, gender,  career, address,mobile,picture);
        //调用业务层，将书籍添加到数据库中
       PersonalFileServiceImpl personalFileService = new PersonalFileServiceImpl();
        boolean result = personalFileService.addPersonalFile(personalFile);

        if (result){
            //添加成功，显示新的列表
            response.sendRedirect(request.getContextPath()+"/PersonalFileList");
        }else {
            //添加失败，重新添加
            session.setAttribute("msg", "添加失败");
            response.sendRedirect(request.getContextPath()+"/addPersonalFileList.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}


